//
//  PayTypeViewController.m
//  AppointmentForCar
//
//  Created by Herron on 2016/11/29.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "PayTypeViewController.h"
#import "AppDelegate.h"
#import "DateTimeManager.h"
#import "WeChatPayModel.h"

@interface PayTypeViewController ()
@property (weak, nonatomic) IBOutlet UILabel *certifiedPriceLabel;
@property (nonatomic, copy) NSString *payType;
@property (weak, nonatomic) IBOutlet UIButton *wechatBtn;
@property (weak, nonatomic) IBOutlet UIButton *alipayBtn;

@end

@implementation PayTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"认证费用支付方式";
    self.payType = @"1"; // 默认使用支付宝支付
    self.certifiedPriceLabel.text = [NSString stringWithFormat:@"¥ %@", self.totalFee];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)payTypeClick:(UIButton *)sender {
    if (sender.tag == 1) {
        self.payType = @"1";
        [sender setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.wechatBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    } else {
        self.payType = @"2";
        [sender setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.alipayBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    }
}


#pragma mark - 确认支付
- (IBAction)confimClick:(id)sender {
    NSDictionary *prameDic = @{@"userId":self.userId, @"totalFee":self.totalFee};
    if ([self.payType isEqualToString:@"1"]) {
        // 获取支付宝支付信息
        NSLog(@"prameDic = %@", prameDic);
        [NetWork postNetWorkWithUrl:kURLAliPay parame:prameDic successBlock:^(NSDictionary *dic) {
            NSLog(@"支付宝dic = %@", dic);
            NSString *orderStr = dic[@"data"][@"payInfo"];
            [self aliPayWithOrderStr:orderStr];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:kMessage_NetError];
        }];
    } else {
        // 获取微信支付信息
        [NetWork postNetWorkWithUrl:kURLWeChatPay parame:prameDic successBlock:^(NSDictionary *dic) {
            NSLog(@"微信dic = %@", dic);
            NSError *error = nil;
            WeChatPayModel *model = [[WeChatPayModel alloc] initWithDictionary:dic[@"data"] error:&error];
            NSLog(@"model = %@, error = %@", model, error);
            [self weChatPayWithPayDic:model];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:kMessage_NetError];
        }];
    }
}

#pragma mark - 支付宝支付
- (void)aliPayWithOrderStr:(NSString *)orderStr {
    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:kAppScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"支付宝reslut = %@",resultDic);
    }];
}

#pragma mark - 微信支付
- (void)weChatPayWithPayDic:(WeChatPayModel *)model {
    PayReq *request = [[PayReq alloc] init];
    /** 商家向财付通申请的商家id */
    request.partnerId = model.partnerId;
    /** 预支付订单 */
    request.prepayId = model.prepayId;
    /** 商家根据财付通文档填写的数据和签名 */
    request.package = model.package;
    /** 随机串，防重发 */
    request.nonceStr= model.nonceStr;
    /** 时间戳，防重发 */
    request.timeStamp = model.timestamp.intValue;
    /** 商家根据微信开放平台文档对数据做的签名 */
    request.sign = model.sign;
    [WXApi sendReq: request];
}


@end
